package com.heima.article.controller.v1;

import com.heima.article.service.IApArticleService;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.common.dtos.ResponseResult;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;

/**
 * @author Gareth Cao
 * @date 2022/6/12 18:27
 */
@RestController
@RequestMapping("/api/v1/article")
public class ArticleHomeController {
    @Autowired
    private IApArticleService apArticleService;

    /**
     * 加载首页，查询所有条件，把最新的十条数据展示出来
     * 上拉加载更多，设置type=1；下拉加载最新，设置type=2
     * type除了不能是1和2，可以是任意值
     *
     * @param dto
     * @return
     */
    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDto dto) {
        //设置加载首页的type，随便赋值
        Integer type = 3;
        List<Article> list = apArticleService.load(dto, type);
        return ResponseResult.okResult(list);
    }

    /**
     * 上拉加载更多，设置type=1，从sql语句中判断type的值，然后去用作动态sql的条件判断
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadmore")
    public ResponseResult loadMore(@RequestBody ArticleHomeDto dto) {
        //设置加载首页的type，随便赋值
        Integer type = 1;
        List<Article> list = apArticleService.load(dto, type);
        return ResponseResult.okResult(list);
    }

    /**
     * 下拉加载最新，设置type=2，从sql语句中判断type的值，然后去用作动态sql的条件判断
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadnew")
    public ResponseResult loadNew(@RequestBody ArticleHomeDto dto) {
        //设置加载首页的type，随便赋值
        Integer type = 2;
        List<Article> list = apArticleService.load(dto, type);
        return ResponseResult.okResult(list);
    }
}
